package com.moon.game.command.guest.chat;

import java.util.LinkedList;
import java.util.List;

import com.moon.game.command.BaseCmd;
import com.moon.game.command.RequestSession;
import com.moon.game.constants.Constants;
import com.moon.game.exception.GException;
import com.moon.game.manager.ItemMG;
import com.moon.game.resource.model.Item;
import com.moon.game.service.ChatService;
import com.moon.game.service.ItemService;
import com.moon.game.util.Utils;
import com.moon.game.vos.MsgVo;

/**
 * @description 查看聊天消息
 * @author Ranger 作者 E-mail:haozi000005@163.com
 * @version 创建时间：2012-5-31 下午2:09:13
 */
public class ViewChatMsgCmd extends BaseCmd {
	@Override
	protected void runOnce(RequestSession req) throws GException {
		switch (req.getStatus()) {
		case STATU_INIT:
			doInit(req);
			break;
		case STATU_CHECK:
			doCheckSelf(req);
			break;
		case STATU_RETURN:
			doResponse(req);
			break;
		default:
			break;
		}
	}
	
	@Override
	protected void doResponse(RequestSession req) throws GException {
		String[] ps = req.getClientRequest().getPs();
		if (ps == null || ps.length < 2) {
			doResponsePsError(req);
			return;
		}
		Integer type = Integer.parseInt(ps[0]);
		Integer page = Integer.parseInt(ps[1]);
		if (type == null || page == null) {
			doResponsePsError(req);
			return;
		}
		LinkedList<MsgVo> chatList = null;
		if (type == Constants.CHAT_WORLD) {
			//世界消息
			chatList = ChatService.getWorldChatList();
		}else if (type == Constants.CHAT_PUBLIC) {
			//公聊消息
			chatList = ChatService.getPublicMessage(getPlayerVo(req).getLocation());
		}else if (type == Constants.CHAT_PRIVATE) {
			//私聊消息
			chatList = ChatService.getPrivateMessage(req.getPlayerId());
		}else if (type == Constants.CHAT_WORLD_ACT) {
			//世界动态消息
			chatList = ChatService.getWorldActChatList();
		}else if (type == Constants.CHAT_PRIVATE_ACT) {
			//私聊动态消息
			chatList = ChatService.getPrivateActMessage(req.getPlayerId());
		}
		
		//分页处理
		LinkedList<MsgVo> showMsg = new LinkedList<MsgVo>();
		List<Object> pageMsg = Utils.page(chatList, page.intValue(), Constants.PAGE_SIZE);
		for (Object obj : pageMsg) {
			MsgVo msg = (MsgVo)obj;
			if (msg == null) {
				continue;
			}
			showMsg.addLast(msg);
		}
		
		
		//总页数
		if (chatList != null && chatList.size() > 0) {
			req.setVO("total_page", Utils.totalPage(chatList.size(), Constants.PAGE_SIZE));
		}
		
		req.setVO("cur_page", page);
		req.setVO("msg_list", showMsg);
		req.setVO("cur_type", type);
		
		
		//小喇叭数量
		Item smallHorn = ItemService.getItem(req.getPlayerId(), Constants.SMALL_HORN_ID);
		int smallHornAmount = 0;
		if (smallHorn != null) {
			smallHornAmount = smallHorn.getNum();
		}
		req.setVO("small_horn_amount", smallHornAmount);
		
		//传音符的数量
		Item talismanSpeaker = ItemService.getItem(req.getPlayerId(), Constants.TALISMAN_SPEAKER_ID);
		int talismanSpeakerAmount = 0;
		if (talismanSpeaker != null) {
			talismanSpeakerAmount = talismanSpeaker.getNum();
		}
		req.setVO("talisman_speaker_amount", talismanSpeakerAmount);
		
		response(req, req.getClientResponse());
	}

}
